#pragma once
#include<iostream>
#include<pthread.h>
#include<string>
#include<functional>

using namespace std;
// using fun = function<void()>;
typedef void(*callback_t)();

int num = 1;

class Thread
{
public:
    static void *Routine(void *args)
    {
        Thread* thread = static_cast<Thread *>(args);
        thread->Entry();
    }

public:
    Thread(callback_t cb)
    :_tid(0)
    ,_name("")
    ,_start_time(0)
    ,_isrunning(false)
    {}

    void Run()
    {
        _name = "thread-"+to_string(num++);
        _start_time = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
        _isrunning = false;
    }

    string GetName()
    {
        return _name;
    }

    uint64_t StartTime()
    {
        return _start_time;
    }

    bool IsRunning()
    {
        return _isrunning;
    }

    void Entry()
    {
        _cb();
    }
    ~Thread()
    {}

private:
    pthread_t _tid;
    string _name;
    uint64_t _start_time;
    bool _isrunning;

    callback_t _cb;
};